package com.ruoyi.syf.service;

import com.ruoyi.syf.domain.vo.RiskAnalysisUnitVo;
import com.ruoyi.syf.domain.vo.RiskEvaluationAccidentDetailVo;
import com.ruoyi.syf.mapper.RiskAnalysisUnitMapper;
import com.ruoyi.syf.mapper.RiskEvaluationAccidentDetailMapper;
import com.ruoyi.syf.mapper.RiskMeasureMapper;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class TreeService {

    private static final String OBJECT = "object";
    private static final String UNIT = "unit";
    private static final String EVALUATION = "evaluation";
    private static final String MEASURE = "measure";

    @Autowired
    private RiskAnalysisUnitMapper unitMapper;
    @Autowired
    private RiskEvaluationAccidentDetailMapper evaluationAccidentDetailMapper;
    @Autowired
    private RiskMeasureMapper measureMapper;

    /**
     * 构建单元树
     */
    public List<TreeNode> unitTree() {
        List<TreeNode> objectTree = new ArrayList<>();
        List<RiskAnalysisUnitVo> list = unitMapper.tree();
        Map<Long, List<RiskAnalysisUnitVo>> objectCollect = list.stream()
                .collect(Collectors.groupingBy(RiskAnalysisUnitVo::getObjectId));
        objectCollect.forEach((objectId, val) -> {
            RiskAnalysisUnitVo unitVo = val.get(0);
            //构建对象节点
            TreeNode objectNode = new TreeNode(objectId.toString(), unitVo.getObjectName(), null, OBJECT);
            List<TreeNode> unitTree = new ArrayList<>();
            val.forEach(e -> {
                //构建单元节点
                TreeNode unitNode = new TreeNode(e.getId().toString(), e.getName(), objectId.toString(), UNIT);
                unitTree.add(unitNode);
            });
            objectNode.setChildren(unitTree);
            objectTree.add(objectNode);
        });
        return objectTree;
    }

    /**
     * 构建风险树
     */
    public List<TreeNode> evaluationTree(){
        List<TreeNode> objectTree = new ArrayList<>();
        List<RiskEvaluationAccidentDetailVo> list = evaluationAccidentDetailMapper.tree();
        Map<Long, List<RiskEvaluationAccidentDetailVo>> objectCollect = list.stream()
                .collect(Collectors.groupingBy(RiskEvaluationAccidentDetailVo::getObjectId));
        objectCollect.forEach((objectId, val) -> {
            RiskEvaluationAccidentDetailVo vo = val.get(0);
            //构建对象节点
            TreeNode objectNode = new TreeNode(objectId.toString(), vo.getObjectName(), null, OBJECT);
            List<TreeNode> unitTree = new ArrayList<>();
            Map<Long, List<RiskEvaluationAccidentDetailVo>> unitCollect = val.stream()
                    .collect(Collectors.groupingBy(RiskEvaluationAccidentDetailVo::getUnitId));
            unitCollect.forEach((unitId,val2) -> {
                RiskEvaluationAccidentDetailVo vo2 = val2.get(0);
                //构建单元节点
                TreeNode unitNode = new TreeNode(unitId.toString(), vo2.getUnitName(), objectId.toString(), UNIT);

                //构建风险节点
                unitNode.setChildren(evaluationNode(val2,unitId.toString()));

                unitTree.add(unitNode);
            });
            objectNode.setChildren(unitTree);
            objectTree.add(objectNode);
        });
        return objectTree;
    }

    /**
     * 构建风险节点
     */
    public List<TreeNode> evaluationNode(List<RiskEvaluationAccidentDetailVo> list,String parentId){
        List<TreeNode> evaluationTree = new ArrayList<>();
        list.forEach(e -> {
            TreeNode treeNode2 = new TreeNode(e.getId().toString(), e.getAccidentName(), parentId, EVALUATION);
            evaluationTree.add(treeNode2);
        });
        return evaluationTree;
    }

    @Data
    public static class TreeNode{
        private String id;
        private String name;
        private String parentId;
        private String type;
        // 子节点列表
        private List<TreeNode> children;

        public TreeNode(String id, String name, String parentId, String type) {
            this.id = id;
            this.name = name;
            this.parentId = parentId;
            this.type = type;
        }
    }

}
